package com.leicx.guli.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leicx.guli.common.constant.ParamConstant;
import com.leicx.guli.common.utils.CommonPageQueryUtil;
import com.leicx.guli.common.utils.FeignUtil;
import com.leicx.guli.common.utils.PageUtils;
import com.leicx.guli.common.utils.Query;
import com.leicx.guli.common.utils.R;
import com.leicx.guli.product.dao.SkuInfoDao;
import com.leicx.guli.product.entity.SkuImagesEntity;
import com.leicx.guli.product.entity.SkuInfoEntity;
import com.leicx.guli.product.entity.SpuInfoDescEntity;
import com.leicx.guli.product.feign.SeckillFeignService;
import com.leicx.guli.product.service.AttrGroupService;
import com.leicx.guli.product.service.SkuImagesService;
import com.leicx.guli.product.service.SkuInfoService;
import com.leicx.guli.product.service.SkuSaleAttrValueService;
import com.leicx.guli.product.service.SpuInfoDescService;
import com.leicx.guli.product.vo.sku.item.SeckillSkuVo;
import com.leicx.guli.product.vo.sku.item.SkuItemSaleAttrVo;
import com.leicx.guli.product.vo.sku.item.SkuItemVo;
import com.leicx.guli.product.vo.sku.item.SpuItemAttrGroupVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    SkuImagesService skuImagesService;
    @Resource
    SpuInfoDescService spuInfoDescService;
    @Resource
    AttrGroupService attrGroupService;
    @Resource
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    ThreadPoolExecutor executor;
    @Resource
    SeckillFeignService seckillFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        CommonPageQueryUtil.setKeyQuery(queryWrapper, params, ParamConstant.SKU_ID, "sku_name");
        CommonPageQueryUtil.setNormalEqQuery(queryWrapper, params, "catelogId", "catalog_id");
        CommonPageQueryUtil.setNormalEqQuery(queryWrapper, params, ParamConstant.BRAND_ID);
        CommonPageQueryUtil.setCommonQuery(queryWrapper, params, "min", ParamConstant.PRICE, "ge");
        CommonPageQueryUtil.setCommonQuery(queryWrapper, params, "max", ParamConstant.PRICE, "le");
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return list(new QueryWrapper<SkuInfoEntity>().eq(ParamConstant.SPU_ID, spuId));
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1、sku的基本信息 pms_sku_info
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            // 3、spu的销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            // 4、获取spu的介绍 pms_spu_info_desc
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        }, executor);

        CompletableFuture<Void> groupAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            // 5、获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(
                    res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            // 2、sku的图片信息 pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            //3、远程调用查询当前sku是否参与秒杀优惠活动
            R skuSeckilInfo = seckillFeignService.getSkuSeckillInfo(skuId);
            if (skuSeckilInfo.success()) {
                //查询成功
                SeckillSkuVo seckilInfoData = FeignUtil.formatClass(skuSeckilInfo, SeckillSkuVo.class);
                skuItemVo.setSeckillSkuVo(seckilInfoData);

                if (seckilInfoData != null) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime > seckilInfoData.getEndTime()) {
                        skuItemVo.setSeckillSkuVo(null);
                    }
                }
            }
        }, executor);

        // 等到所有任务都完成
        CompletableFuture.allOf(saleAttrFuture, descFuture, groupAttrFuture, imageFuture, seckillFuture).get();
        return skuItemVo;
    }

}